/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainGui.java
 *
 * Created on Jan 13, 2011, 10:12:19 PM
 */
package MainGui;

import Builder.Builder_v1;
import RuleDatabase.Extractor_21_12_2010;
import RuleDatabase.RuleDatabase;
import RuleDatabase.RuleSet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import jxl.write.WriteException;

/**
 *
 * @author heckarim
 */
public class MainGui extends javax.swing.JFrame {

    private String GenFolder;
    private Builder_v1 Generator = new Builder_v1();
    private LinkedList<LinkedList<String>> listRule = new LinkedList<LinkedList<String>>();
    private boolean isfinishedprocesspcre = false;
    private LinkedList<pcreBRam> listBRam = new LinkedList<pcreBRam>();
    DefaultListModel listModel = new DefaultListModel();
    DefaultListModel lmRules = new DefaultListModel();
    private String RulesFolder = null;
    private String outVRTFolder = null;
    private RuleDatabase rdb = new RuleDatabase();

    /**
     * this function inchare of update jlist brams
     */
    private void doUpdateBramList() {
        listModel.clear();
        for (int i = 0; i < this.listBRam.size(); i++) {
            this.listBRam.get(i).order = i;
        }
        for (int i = 0; i < this.listBRam.size(); i++) {
            listModel.addElement(this.listBRam.get(i));
        }
        this.jlstBRam.setSelectedIndex(0);
    }

    private void updateListRules() {
        this.lmRules.clear();
        for (int i = 0; i < rdb.lstSnortRuleSet.size(); i++) {
            this.lmRules.addElement(rdb.lstSnortRuleSet.get(i));
        }
    }

    private void createLookAndFeel() {
        try {
            // Set cross-platform Java L&F (also called "Metal")
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void loadAboutPanel() {
        //this.jtxtaThesisTopic.setBackground(this.getBackground());

        this.jPanel1.setVisible(false);
        this.jPanel2.setVisible(false);
        this.jpAbout.setVisible(true);
        this.jpAbout.setLocation(5, 5);
        this.setBounds(this.getX(), this.getY(), this.jpAbout.getWidth() + 40, this.jpAbout.getHeight() + 80);

    }

    /**
     *  special claff or manage BRam
     */
    class pcreBRam {

        int order = 0; //oder of BRam
        LinkedList<String> listPcre = new LinkedList<String>();

        public void addpcre(String pcre) {
            this.listPcre.add(pcre);
        }

        public String toString() {
            return "BRAM " + order;
        }
    }

    /** Creates new form MainGui */
    public MainGui() {
        initComponents();
        //createlook and feel
        this.createLookAndFeel();
        //load default panel
        this.loadAboutPanel();
        //for About:
        //For rules extractor
        this.jlstRuleset.setModel(lmRules);
        this.jtxtfVRTDir.setText(System.getProperty("user.dir"));
        //For pcre builder
        this.jlstBRam.setModel(listModel);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jtxtfVRTDir = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jbtnVRTDir = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jlstRuleset = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        jtxtaRule = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jtxtfOutVRT = new javax.swing.JTextField();
        jbtnOutVRT = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jbtnRulefile = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jbtnOutputDir = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtxtaPcre = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jlstBRam = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jbtnAdd = new javax.swing.JButton();
        jbtnDel = new javax.swing.JButton();
        jbtnUpdate = new javax.swing.JButton();
        jbtnGenHdl = new javax.swing.JButton();
        jpAbout = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jmiRuleDialog = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jmiAbout = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("RuleExtractor"));

        jLabel5.setText(" VRT Dir");

        jbtnVRTDir.setText("...");
        jbtnVRTDir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnVRTDirActionPerformed(evt);
            }
        });

        jlstRuleset.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jlstRuleset.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jlstRulesetValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(jlstRuleset);

        jtxtaRule.setColumns(20);
        jtxtaRule.setRows(5);
        jScrollPane4.setViewportView(jtxtaRule);

        jButton2.setText("Extract Rules");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel6.setText(" Output Dir");

        jbtnOutVRT.setText("...");
        jbtnOutVRT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnOutVRTActionPerformed(evt);
            }
        });

        jButton1.setText("Check Rules");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 520, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtxtfOutVRT)
                            .addComponent(jtxtfVRTDir, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jbtnOutVRT)
                            .addComponent(jbtnVRTDir))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jtxtfVRTDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnVRTDir))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtfOutVRT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnOutVRT)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, -1, 490));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("HDLBuilder"));

        jbtnRulefile.setText("...");
        jbtnRulefile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRulefileActionPerformed(evt);
            }
        });

        jLabel1.setText("pcre File:");

        jLabel2.setText("output Dir:");

        jbtnOutputDir.setText("...");
        jbtnOutputDir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnOutputDirActionPerformed(evt);
            }
        });

        jtxtaPcre.setColumns(20);
        jtxtaPcre.setRows(5);
        jScrollPane1.setViewportView(jtxtaPcre);

        jlstBRam.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jlstBRam.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jlstBRamValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jlstBRam);

        jLabel3.setText("PCRE List:");

        jLabel4.setText("BRam List:");

        jbtnAdd.setText("Add");
        jbtnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAddActionPerformed(evt);
            }
        });

        jbtnDel.setText("Delete");
        jbtnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDelActionPerformed(evt);
            }
        });

        jbtnUpdate.setText("Update PCREs");
        jbtnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnUpdateActionPerformed(evt);
            }
        });

        jbtnGenHdl.setText("Generate HDLs");
        jbtnGenHdl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGenHdlActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 512, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jbtnAdd)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
                                .addComponent(jbtnDel))
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                            .addComponent(jbtnGenHdl, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                            .addComponent(jbtnUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jTextField2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtnOutputDir, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 543, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtnRulefile, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnRulefile)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnOutputDir)
                    .addComponent(jLabel2))
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbtnAdd)
                            .addComponent(jbtnDel))
                        .addGap(27, 27, 27)
                        .addComponent(jbtnUpdate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnGenHdl)))
                .addGap(26, 26, 26))
        );

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 600, -1, 520));

        jpAbout.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jpAbout.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel7.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabel7.setText("Software ToolChain");
        jpAbout.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 20, 260, 37));

        jLabel8.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel8.setText("       Research, Design and Implement Regular Expression");
        jpAbout.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, 520, 28));

        jLabel9.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel9.setText("     module on FPGA for Network Instrusion Detect System");
        jpAbout.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 520, -1));

        jLabel10.setText("Thesis Topics:");
        jpAbout.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, 140, -1));

        jLabel11.setText("Students:");
        jpAbout.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 210, 90, -1));

        jLabel12.setFont(new java.awt.Font("Times New Roman", 1, 13));
        jLabel12.setText("Tran Trung Hieu          50600712");
        jpAbout.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 230, 200, -1));

        jLabel13.setFont(new java.awt.Font("Times New Roman", 1, 13));
        jLabel13.setText("Le Hoang Long             50601302");
        jpAbout.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 250, 200, -1));

        jLabel14.setFont(new java.awt.Font("Times New Roman", 1, 13));
        jLabel14.setText("Vu Tan Tai                     50602091");
        jpAbout.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 270, 210, -1));

        jLabel15.setFont(new java.awt.Font("Times New Roman", 1, 13));
        jLabel15.setText("Eng.  Nguyen Hoa Hung");
        jpAbout.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 340, -1, -1));

        jLabel16.setFont(new java.awt.Font("Times New Roman", 1, 13));
        jLabel16.setText("Dr.    Tran Ngoc Thinh");
        jpAbout.add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 320, -1, -1));

        jLabel17.setText("Instructors:");
        jpAbout.add(jLabel17, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 290, 100, -1));

        jLabel18.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel18.setText("       1/2011");
        jpAbout.add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 60, 90, -1));

        getContentPane().add(jpAbout, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 1160, 510, 380));
        jpAbout.getAccessibleContext().setAccessibleParent(jpAbout);

        jMenu1.setText("Tools");

        jmiRuleDialog.setText("RuleExtractor     ");
        jmiRuleDialog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiRuleDialogActionPerformed(evt);
            }
        });
        jMenu1.add(jmiRuleDialog);

        jMenuItem1.setText("HDL Builder");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("About");
        jMenu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu2ActionPerformed(evt);
            }
        });

        jmiAbout.setText("Information    ");
        jmiAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiAboutActionPerformed(evt);
            }
        });
        jMenu2.add(jmiAbout);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jmiRuleDialogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiRuleDialogActionPerformed
        this.jPanel2.setVisible(false);
        this.jPanel1.setVisible(true);
        this.jPanel1.setLocation(5, 5);
        this.setBounds(this.getX(), this.getY(), this.jPanel1.getWidth() + 25, this.jPanel1.getHeight() + 80);
    }//GEN-LAST:event_jmiRuleDialogActionPerformed

    private void jbtnRulefileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRulefileActionPerformed
        JFileChooser filechooser = new JFileChooser(System.getProperty("user.dir"));

        int returnVal = filechooser.showOpenDialog(null);
        File file;
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = filechooser.getSelectedFile();
            if (file.exists() && file.isFile()) {
                this.jTextField1.setText(file.getAbsolutePath());
                this.doLoadFile(file);
                this.doUpdateBramList();
                //this.Generator.pcrefile = file.getAbsolutePath();
            }
        } else {
            file = null;
        }
    }//GEN-LAST:event_jbtnRulefileActionPerformed

    private void jlstBRamValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jlstBRamValueChanged
        if (evt.getValueIsAdjusting() == false) {
            int index = this.jlstBRam.getSelectedIndex();
            if (index == -1) {
                //No selection, disable fire button.
            } else {
                pcreBRam bram = (pcreBRam) this.listModel.get(index);
                //update pcre lists
                this.jtxtaPcre.setText("");
                for (int i = 0; i < bram.listPcre.size(); i++) {
                    this.jtxtaPcre.insert(bram.listPcre.get(i) + "\n", this.jtxtaPcre.getCaretPosition());
                }
            }
        }
    }//GEN-LAST:event_jlstBRamValueChanged
    /**
     *
     * @param evt
     *
     * add new Bram in to bram list
     */
    private void jbtnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAddActionPerformed
        pcreBRam bram = new pcreBRam();
        this.listBRam.add(bram);
        //update bram
        this.doUpdateBramList();
    }//GEN-LAST:event_jbtnAddActionPerformed

    private void jbtnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDelActionPerformed
        if (this.jlstBRam.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(null, "Please choose one entity!!", "Delete Warning", JOptionPane.WARNING_MESSAGE);
        } else {
            pcreBRam bram = (pcreBRam) this.listModel.get(this.jlstBRam.getSelectedIndex());
            this.listBRam.remove(bram);
            this.doUpdateBramList();
        }
    }//GEN-LAST:event_jbtnDelActionPerformed

    private void jbtnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnUpdateActionPerformed
        if(this.jlstBRam.getSelectedIndex() == -1 )
            return;

        //update current pcrelist
        pcreBRam bram = (pcreBRam) this.listModel.get(this.jlstBRam.getSelectedIndex());
        bram.listPcre.clear();
        //update
        String[] split = this.jtxtaPcre.getText().split("\n");
        for (int i = 0; i < split.length; i++) {
            String s = split[i].trim();
            if (s.isEmpty()) {
                continue;
            }
            if (s.startsWith("#")) {
                continue;
            }
            bram.addpcre(s);
        }
        this.doUpdateBramList();
    }//GEN-LAST:event_jbtnUpdateActionPerformed

    private void jbtnGenHdlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGenHdlActionPerformed

        //check output folder
        if (this.jTextField2.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Please specify Output floder!", "HDL Builder", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (!this.jTextField2.getText().trim().endsWith(File.separator)) {
            this.GenFolder = this.jTextField2.getText().trim() + File.separator;
        } else {
            this.GenFolder = this.jTextField2.getText().trim();
        }
        //update Genfolder
        this.Generator.genDir = this.GenFolder;
        //update rule to gen.
        //conver lisst bramto listrule
        this.listRule.clear();
        for (int i = 0; i < this.listBRam.size(); i++) {
            LinkedList<String> list = new LinkedList<String>();
            pcreBRam bram = this.listBRam.get(i);
            if (bram.listPcre.isEmpty()) {
                continue;
            }
            for (int j = 0; j < bram.listPcre.size(); j++) {
                list.add(bram.listPcre.get(j));
            }
            this.listRule.add(list);
        }
        // update list rule
        this.Generator.listRule = this.listRule;
        this.Generator.buildRTL();
        this.Generator.genHDL();
        JOptionPane.showMessageDialog(null, "Finish generate HDL!!!", "HDL Builder", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jbtnGenHdlActionPerformed

    private void jbtnOutputDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnOutputDirActionPerformed
        JFileChooser filechooser = new JFileChooser(new File(System.getProperty("user.dir")));
        filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = filechooser.showOpenDialog(null);
        File file;
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = filechooser.getSelectedFile();
            if (file.exists() && file.isDirectory()) {
                this.jTextField2.setText(file.getAbsolutePath());
                this.GenFolder = file.getAbsolutePath() + File.separator;
                this.Generator.genDir = this.GenFolder;
            }
        } else {
            file = null;
        }
    }//GEN-LAST:event_jbtnOutputDirActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.jPanel1.setVisible(false);
        this.jPanel2.setVisible(true);
        this.jPanel2.setLocation(5, 5);
        this.setBounds(this.getX(), this.getY(), this.jPanel2.getWidth() + 15, this.jPanel2.getHeight() + 50);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jbtnOutVRTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnOutVRTActionPerformed
        JFileChooser filechooser = new JFileChooser(new File(System.getProperty("user.dir")));
        filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = filechooser.showOpenDialog(null);
        File file;
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = filechooser.getSelectedFile();
            if (file.exists() && file.isDirectory()) {
                this.jtxtfOutVRT.setText(file.getAbsolutePath());
                this.outVRTFolder = file.getAbsolutePath() + File.separator;
            }
        } else {
            file = null;
        }
    }//GEN-LAST:event_jbtnOutVRTActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (this.jtxtfVRTDir.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Please specify VRT Rules Folder!!!", "Rules Check", JOptionPane.WARNING_MESSAGE);
            return;
        } else {
            this.RulesFolder = this.jtxtfVRTDir.getText();
            rdb.setRuleFolder(this.RulesFolder);
            rdb.BuildDatabase();
            this.updateListRules();
            System.out.println("finish check rules");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jlstRulesetValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jlstRulesetValueChanged
        if (evt.getValueIsAdjusting() == false) {
            int index = this.jlstRuleset.getSelectedIndex();
            if (index == -1) {
                //No selection, disable fire button.
                //do nothings
            } else {
                RuleSet rs = rdb.lstSnortRuleSet.get(index);
                if (rs != null) {
                    this.jtxtaRule.setText("");
                }
                for (int i = 0; i < rs.lstRuleAll.size(); i++) {
                    this.jtxtaRule.insert(rs.lstRuleAll.get(i).value + "\n", this.jtxtaRule.getCaretPosition());
                }
            }
        }
    }//GEN-LAST:event_jlstRulesetValueChanged

    private void jbtnVRTDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnVRTDirActionPerformed
        JFileChooser filechooser = new JFileChooser(new File(System.getProperty("user.dir")));
        filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = filechooser.showOpenDialog(null);
        File file;
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = filechooser.getSelectedFile();
            if (file.exists() && file.isDirectory()) {
                this.jtxtfVRTDir.setText(file.getAbsolutePath());
                this.RulesFolder = file.getAbsolutePath() + File.separator;
            }
        } else {
            file = null;
        }
    }//GEN-LAST:event_jbtnVRTDirActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (this.jtxtfOutVRT.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Please sepcify ouput folder!!!", "Output rules", JOptionPane.WARNING_MESSAGE);
            return;
        } else {
            Extractor_21_12_2010 ext = new Extractor_21_12_2010();
            ext.setOuputFolder(this.outVRTFolder);
            ext.setRuleDatabase(this.rdb);
            try {
                ext.outputExcel();
                System.out.println("finish extract rule and infomation");
                JOptionPane.showMessageDialog(null, "Finished!!!", "Rule Extractor", JOptionPane.INFORMATION_MESSAGE);
            } catch (IOException ex) {
                Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);
            } catch (WriteException ex) {
                Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu2ActionPerformed
//        this.loadAboutPanel();
    }//GEN-LAST:event_jMenu2ActionPerformed

    private void jmiAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiAboutActionPerformed
        this.loadAboutPanel();
    }//GEN-LAST:event_jmiAboutActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainGui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JButton jbtnAdd;
    private javax.swing.JButton jbtnDel;
    private javax.swing.JButton jbtnGenHdl;
    private javax.swing.JButton jbtnOutVRT;
    private javax.swing.JButton jbtnOutputDir;
    private javax.swing.JButton jbtnRulefile;
    private javax.swing.JButton jbtnUpdate;
    private javax.swing.JButton jbtnVRTDir;
    private javax.swing.JList jlstBRam;
    private javax.swing.JList jlstRuleset;
    private javax.swing.JMenuItem jmiAbout;
    private javax.swing.JMenuItem jmiRuleDialog;
    private javax.swing.JPanel jpAbout;
    private javax.swing.JTextArea jtxtaPcre;
    private javax.swing.JTextArea jtxtaRule;
    private javax.swing.JTextField jtxtfOutVRT;
    private javax.swing.JTextField jtxtfVRTDir;
    // End of variables declaration//GEN-END:variables

    private void doLoadFile(File file) {
        LinkedList<String> listloadstring = new LinkedList<String>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s;
            while ((s = br.readLine()) != null) {
                //this.doc.in
                // this.textArea1.insert(s + "\n", this.textArea1.getCaretPosition());
                if (!s.trim().isEmpty()) {
                    listloadstring.add(s);
                }
            }
            br.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
        //update listBRam information

        this.listBRam = new LinkedList<pcreBRam>();
        pcreBRam bram = new pcreBRam();
        for (int i = 0; i < listloadstring.size(); i++) {
            String s = listloadstring.get(i);
            if (s.startsWith("#bram")) {
                if (!bram.listPcre.isEmpty()) {
                    //add all bram and create new bram
                    this.listBRam.add(bram);
                    bram = new pcreBRam();
                }
            } else {
                if (s.startsWith("#")) {
                    continue;
                }
                bram.addpcre(s);
            }
        }
        if (!bram.listPcre.isEmpty()) {
            this.listBRam.add(bram);
        }
        //update order
        for (int i = 0; i < this.listBRam.size(); i++) {
            this.listBRam.get(i).order = i;
        }
    }
}
